#pragma once
#include "Fileds.hpp"
#include <memory>
#include <functional>

namespace zgwrpc
{
    // 定义消息抽象类
    class BaseMessage
    {
    public:
        using ptr = std::shared_ptr<BaseMessage>;

        virtual ~BaseMessage() {};
        virtual void setId(const std::string &id) { _rid = id; } // 设置rid
        virtual std::string rid() { return _rid; }               // 返回rid

        virtual void setMType(MType mtype) { _mtype = mtype; } // 设置消息类型
        virtual MType mtype() { return _mtype; }               // 返回消息类型
        virtual std::string serialize() = 0;                   // 序列化消息
        virtual bool unSerialize(std::string &data) = 0;       // 反序列化消息

        virtual bool check() = 0; // 检查消息是否合法

    protected:
        MType _mtype;
        std::string _rid; // 请求的id
    };

    // 定义缓冲区抽象类
    class BaseBuffer
    {
    public:
        using ptr = std::shared_ptr<BaseBuffer>;

        virtual ~BaseBuffer() {}
        virtual size_t readableSize() = 0;                   // 返回可读字节数
        virtual int32_t peekInt32() = 0;                     //  跳过字节并返回int32_t类型数据
        virtual void retrieveInt32() = 0;                    // 跳过32字节
        virtual int32_t readInt32() = 0;                     // 读取int32_t类型数据
        virtual std::string retrievAsString(size_t len) = 0; // 返回指定长度的字符串，并跳过相应字节
    };

    // 定义协议抽象类
    class BaseProtocol
    {
    public:
        using ptr = std::shared_ptr<BaseProtocol>;
        virtual ~BaseProtocol() {};
        virtual bool canProcessed(const BaseBuffer::ptr &buf) = 0;                     // 检查是否可以处理数据
        virtual bool onMessage(BaseMessage::ptr &msg, const BaseBuffer::ptr &buf) = 0; // 处理消息

        virtual std::string serialize(const BaseMessage::ptr &msg) = 0; // 序列化消息
    };

    // 定义连接抽象类
    class BaseConnection
    {
    public:
        using ptr = std::shared_ptr<BaseConnection>;
        virtual ~BaseConnection() {};
        virtual void send(const BaseMessage::ptr &msg) = 0;
        virtual void shutdown() = 0;
        virtual void reset() = 0;
        virtual bool connected() = 0;
    };

    using ConnectionCallback = std::function<void(const BaseConnection::ptr &conn)>;
    using MessageCallback = std::function<void(const BaseConnection::ptr &conn, const BaseMessage::ptr &buf)>;
    using CloseCallback = std::function<void(const BaseConnection::ptr &conn)>;

    // 定义抽象服务类
    class BaseServer
    {
    public:
        using ptr = std::shared_ptr<BaseServer>;
        virtual ~BaseServer() {};

        virtual void setConnectionCallback(const ConnectionCallback &cb) { _connectCallback = cb; } // 设置连接回调
        virtual void setCloseCallback(const CloseCallback &cb) { _closeCallback = cb; }             // 设置关闭回调
        virtual void setMessageCallback(const MessageCallback &cb) { _messageCallback = cb; }       // 设置消息回调
        virtual void start() = 0;                                                                   // 启动服务                                                                 // 停止服务

    protected:
        ConnectionCallback _connectCallback;
        CloseCallback _closeCallback;
        MessageCallback _messageCallback;
    };

    class BaseClient
    {
    public:
        using ptr = std::shared_ptr<BaseClient>;
        virtual void setConnectionCallback(const ConnectionCallback &cb) { _connectCallback = cb; } // 设置连接回调
        virtual void setCloseCallback(const CloseCallback &cb) { _closeCallback = cb; }             // 设置关闭回调
        virtual void setMessageCallback(const MessageCallback &cb) { _messageCallback = cb; }       // 设置消息回调

        virtual BaseConnection::ptr connection() = 0;       // 返回连接对象
        virtual void send(const BaseMessage::ptr &msg) = 0; // 发送消息

        virtual void connect() = 0;     // 连接服务
        virtual bool isConnected() = 0; // 是否连接成功
        virtual void shutdown() = 0;    // 关闭连接
        protected:
        ConnectionCallback _connectCallback;
        CloseCallback _closeCallback;
        MessageCallback _messageCallback;
    };
}
